
//
//  WBEmoticonToolBar.swift
//  微博(swift)
//
//  Created by 汤米 on 16/4/25.
//  Copyright © 2016年 汤米. All rights reserved.
//

import UIKit


// MARK: -  设置button tag 使用
let tagNum = 2000

// MARK: -  button的枚举
enum WBEmoticonToolBarType: Int {
    
    case defaultButton = 0
    case emojiButton = 1
    case flowerButton = 2
    
}
class WBEmoticonToolBar: UIView {
    
    // MARK: -  button被点击之后执行的闭包
    var emoticonToolBarButtonClickClosure: ((type: WBEmoticonToolBarType) -> ())?
    
    // MARK: -  记录被选中的button
    var currentSelectButton: UIButton?

    // MARK: -  重写init方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setUpUI()
    }

    // MARK: -  重写init方法时必须实现的方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: -  设置UI
    func setUpUI() {
        
        let defaultButton = addChildButton(imageName: "compose_emotion_table_left", title: "默认", type: WBEmoticonToolBarType.defaultButton)
        let emojiButton = addChildButton(imageName: "compose_emotion_table_mid", title: "Emoji", type: WBEmoticonToolBarType.emojiButton)
        let flowerButton = addChildButton(imageName: "compose_emotion_table_right", title: "浪小花", type: WBEmoticonToolBarType.flowerButton)
        
        defaultButton.snp_makeConstraints { (make) -> Void in
            
            make.bottom.top.leading.equalTo(self)
            make.width.equalTo(emojiButton)
        }
        
        emojiButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(self)
            make.leading.equalTo(defaultButton.snp_trailing)
            make.width.equalTo(flowerButton)
        }
        
        flowerButton.snp_makeConstraints { (make) -> Void in
        
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emojiButton.snp_trailing)
        }
        
        defaultButton.selected = true
        currentSelectButton = defaultButton
       
    
    }
    
    // MARK: -  设置当前被选中按钮
    func setEmoticonCurrentButton(section: Int) {
        
        let button = viewWithTag(section + tagNum) as! UIButton
        
        clickButton(button)
    }
    
    // MARK: -  button的点击事件
    func clickButton(button: UIButton) {
        
        
        if currentSelectButton == button {

            return
        }
        button.selected = true
        currentSelectButton?.selected = false
        currentSelectButton = button
        
        
        //执行闭包
        emoticonToolBarButtonClickClosure?(type: WBEmoticonToolBarType(rawValue: button.tag - tagNum)!)
    }
    
    // MARK: -  抽取添加button的公共方法
    func addChildButton(imageName imageName: String, title: String, type: WBEmoticonToolBarType) -> UIButton{
        
        let button = UIButton()
        
        button.tag = type.rawValue + tagNum
        button.addTarget(self, action: "clickButton:", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setBackgroundImage(UIImage(named: "\(imageName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "\(imageName)_selected"), forState: UIControlState.Selected)
        
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        addSubview(button)
        return button
    }
}
